var page = 1;
var type = "consume";
$(function () {
    var channel = getQueryString("channel");
    getConsumeRecode("consume");
    $(".back-wrapper").click(function () {
        location.href = "./mine.html?channel="+channel;
    });
    $(".home-wrapper").click(function () {
        location.href = "../index.html?channel="+channel;
    });
    $(".title-wrapper .consume-type").click(function () {
        $(this).addClass("active").siblings(".consume-type").removeClass("active");
        if($(this).attr("data-type") == "read"){
            $(".consume-content ul li").remove();
            // 请求阅读消费记录
            page = 1;
            type = "consume";
            getConsumeRecode("consume");
        }else{
            $(".consume-content ul li").remove();
            // 请求打赏消费记录
            page = 1;
            type = "prop";
            getConsumeRecode("prop");
        }
    });
    scrollEvent();
});

function getConsumeRecode(type) {
    $.ajax({
        type:'get',
        url:BASE_URl + "record/" + type + "?page="+page,
        success:function (data) {
            if(data.success){
                $(".load-wrapper").hide();
                page ++;
                DrawRecode(data.data,type);
            }
        }
    });
}

function DrawRecode(data,type) {
    if(data.length != 0){
        var html = "";
        var num = "";
        if(type != "consume"){
            num = "*1"
        }
        for(var i = 0;i < data.length;i ++){
            html += `<li class="border-1px">
                        <div class="title-and-prod clearfix">
                            <div class="title fl">${data[i].title}</div>
                            <div class="prod fr tr">${data[i].prod}</div>
                        </div>
                        <div class="desc-and-date clearfix">
                            <div class="desc fl">${data[i].desc}${num}</div>
                            <div class="date fr tr">${data[i].date}</div>
                        </div>
                    </li>`;
        }
        $(".consume-content ul").append(html);
    }else{
        $(".nor-more").show();
    }
}

function scrollEvent() {
    window.onscroll = function(){
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;  //获取滚动的距离
        var totalHeight = $(".consume-wrapper").height();
        var clientHeight = document.body.clientHeight && document.documentElement.clientHeight;
        console.log(totalHeight,clientHeight,scrollTop);
        if(scrollTop + clientHeight == totalHeight){
            $(".nor-more").hide();
            $(".load-wrapper").show();
            getConsumeRecode(type);
        }
    };
}